<?php

namespace App\Admin\Metrics\Home;

use App\Models\DayStatisticsModel;
use App\Models\User;
use Dcat\Admin\Widgets\ApexCharts\Chart;
class Statistics extends Chart
{
    public function __construct($containerSelector = null, $options = [])
    {
        parent::__construct($containerSelector, $options);

        $this->setUpOptions();
    }
    /**
     * 初始化图表配置
     */
    protected function setUpOptions()
    {
        $this->options([
            'title' => [
                'text' => '用户数据',
                'left' => 'center',
            ],
            'chart' => [
                'type' => 'line',
                'background' => '#ffffff', // 设置图表背景色为白色
                'offsetX' => 0, // 设置图表左边距
                'height' => 450,
                'toolbar'=>[
                    'show'=>false
                    ]
                ],
            'plotOptions' => [
                'bar' => [
                    'horizontal' => true,
                    'dataLabels' => [
                        'position' => 'top',
                    ],
                ]
            ],
            'legend' => [
//                'data' => ['新增用户', '活跃用户'],
//                'bottom' => 30, // 可以自定义位置
                'show'=>true,
//                'horizontalAlign'=>'right',
                'labels'=> [
//                    'useSeriesColors'=>true
                ],

            ],

            'dataLabels' => [
                'enabled' => true,
                'offsetY' => 0,
                'offsetX' => 5,
                'style' => [
                    'fontSize' => '12px',
                    'colors' => ['#2A8BE4','#41B665']
                ]
            ],
            'stroke' => [
                'curve'=> 'straight',
                'show' => true,
                'width' => 2,
                'colors' =>['#2A8BE4','#41B665']
            ],
            'xaxis' => [
                'categories' => [],
            ],
        ]);
    }

    /**
     * 处理图表数据
     */
    protected function buildData()
    {
        //29天前的数据
        $day_statistics = DayStatisticsModel::query()->where('day','>=',now()->subDays(29))->orderBy('day')->get();
        //当日的数据
        $day =now()->format('Y-m-d');
        $newUser = User::query()->whereDate('created_at',$day)->count();
        // 查询指定日期注册并在当天报名的用户总数
        $new_active_user = User::whereDate('user.created_at', $day)
            ->join('sign_up_job_position', 'user.id', '=', 'sign_up_job_position.user_id')
            ->whereDate('sign_up_job_position.created_at', $day)
            ->distinct('user.id')
            ->count('user.id');
        $data1 = [];
        $data2 = [];
        $categories =[];

        foreach ($day_statistics as $statistic){
            $data1[] = $statistic['new_user'];
            $data2[] = $statistic['new_active_user'];
//            $categories[] = $statistic->day;
            $categories[] = now()->parse($statistic->day)->format('m-d');
        }
        $data1[]=$newUser;
        $data2[]=$new_active_user;
        $categories[]=now()->format('m-d');
        // 执行你的数据查询逻辑
        $data = [
            [
                'name'=>'新增用户',
                'data' => $data1,
//                'type'=>'line',
//                'color'=> "#EE6D7A",
            ],
            [
                'name'=>'活跃用户',
                'data' => $data2
            ]
        ];

        $this->withData($data);
        $this->withCategories($categories);
    }

    /**
     * 设置图表数据
     *
     * @param array $data
     *
     * @return $this
     */
    public function withData(array $data)
    {
        return $this->option('series', $data);
    }

    /**
     * 设置图表类别.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withCategories(array $data)
    {
        return $this->option('xaxis.categories', $data);
    }

    /**
     * 渲染图表
     *
     * @return string
     */
    public function render()
    {
        $this->buildData();

        return parent::render();
    }
}
